package servicectl

import (
	"github.com/spf13/afero"
	"gitlab.local/golibrary/utils"
	"os"
)

const (
	dir    = `/etc/sc.d`
	appDir = "/etc/app.d"
	initrd = "/etc/init.d"
)

type ReStarter interface {
	ReStart(name string) error
}

type ReLoader interface {
	ReLoad(name string) error
}

type Starter interface {
	Start(name string) error
}

type Stopper interface {
	ForceStop(name string) error
	Stop(name string) error
}

type StartStopper interface {
	Starter
	Stopper
}

type ReStartStopper interface {
	ReStarter
	Stopper
}

type StartupsGetter interface {
	GetAutoStartupServices() ([]string, error)
}

type ServiceGetStopper interface {
	Stopper
	StartupsGetter
}

type AppService interface {
	StartupsGetter
	SysService(event bool, exclude ...string) error
	AppService(event bool, exclude ...string) error
	AppServiceByVolume(volume string, event bool, exclude ...string) error
	Stopper
	Starter
}

type RunningStatusGetter interface {
	IsRunning(name string) bool
	IsRunningWithName(name string) bool
}

type StartupSetter interface {
	SetStartup(name string, ok bool) error
}

type ScriptBuilder interface {
	BuildScript(startOrStop bool, name string) string
}

type Ctl interface {
	Starter
	Stopper
	ReStarter
	ReLoader
	RunningStatusGetter
	SetStartup(name string, ok bool) error
	//TODO:fixed method name, above are service name,this one is exe name which is not consistent
	RunningStatusGetter
	ScriptBuilder
	AppService
}

type service struct {
	fs afero.Fs
}

func New(fs afero.Fs) *service {
	if !utils.Exists(appDir) {
		_ = os.Mkdir(appDir, 0700)
	}
	return &service{
		fs: fs,
	}
}
